﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net;

namespace PrinteMaster
{
    public partial class 客服窗口 : Form
    {
        public 客服窗口()
        {
            InitializeComponent();
        }
        private 主窗口 m_FatherWindow;
        private string m_ManagerName;
        /// <summary>
        /// 静态创建窗口的函数
        /// </summary>
        /// <param name="lpstrName"></param>
        /// <param name="window"></param>
        /// <returns></returns>
        public static 客服窗口 create(string lpstrName, 主窗口 window)
        {
            客服窗口 k = new 客服窗口();
            k.m_FatherWindow = window;
            k.Text = k.Text + " ["+ lpstrName + " ]";
            k.Show(window);
            k.m_ManagerName = lpstrName;
            return k;
        }

        private void 客服窗口_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 打印状态的枚举值
        /// </summary>
        public enum PrintState:int
        {
            /// <summary>
            /// 开始排队
            /// </summary>
            start=0,
            /// <summary>
            /// 开始打印
            /// </summary>
            print_start,
            /// <summary>
            /// 打印结束
            /// </summary>
            print_end,
            /// <summary>
            /// 队列中
            /// </summary>
            quaue

        }
        /// <summary>
        /// 打印数据结构体
        /// </summary>
        public struct PrintDate
        {
            /// <summary>
            /// 用户账号，qq号
            /// </summary>
            public string id;
            /// <summary>
            /// 用户名，注册时使用姓名
            /// </summary>
            public string name;
            /// <summary>
            /// 状态机,由PrintState 枚举值常量组成
            /// </summary>
            public PrintState state;
            /// <summary>
            /// 剩余时间：秒为单位，只有 state== PrintState.start 时有效
            /// </summary>
            public int sec;
        }
        private List<PrintDate> m_Quaue = new List<PrintDate>();
        private int lpThis = -1;
        public int sendMessage(PrintDate pd)
        {
            switch (pd.state)
            {
                case PrintState.start:
                    break;
                case PrintState.print_end:
                    break;
                case PrintState.print_start:
                    break;
                case PrintState.quaue:
                    break;
            }
            
            m_Quaue.Add(pd);
            lpThis++;
            return lpThis;
            //return 0;
        }
        /// <summary>
        /// 改变一个消息队列中的消息的状态信息
        /// </summary>
        /// <param name="pd"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool changeMessage(PrintDate pd,int id)
        {
            return false;
        }

        public bool changeMessage(PrintState state,int id)
        {
            PrintDate pd = m_Quaue[id];
            pd.state = state;
            return changeMessage(pd,id);
        }

        private void 客服窗口_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void 客服窗口_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (MessageBox.Show("真的要退出程序吗？", "退出程序", MessageBoxButtons.OKCancel) == DialogResult.OK) 
                System.Environment.Exit(0);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            充值窗口.create("", this);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void timer1sec_Tick(object sender, EventArgs e)
        {
            
            Ping ping = new Ping();
            PingReply result = ping.Send("localhost");
            if (result.Status != IPStatus.Success)
                labelLag.Text = "服务器连接异常!";
            else
                labelLag.Text = "Ping:" + result.RoundtripTime.ToString() + " ms";

            DataSet ds = MySqlHelper.GetDataSet(MySqlHelper.Conn, CommandType.Text, "select * from 打印队列表", null);
            dgv.DataSource = ds.Tables[0].DefaultView;
        }

    }
}
